1 module hip.game2d.loading_bar;
2 
3 class LoadingBar
4 {
5     import hip.api;
6     int x, y;
7     private float percentage = 0;
8     private int width, height, borderSize;
9     HipColor fgColor, bgColor;
10 
11 
12     this(int width, int height, int borderSize, HipColor fgColor = color(50, 200, 20), HipColor bgColor = color(40,40,40))
13     {
14         this.width = width;
15         this.height = height;
16         this.borderSize = borderSize;
17         this.bgColor = bgColor;
18         this.fgColor = fgColor;
19     }
20 
21     static LoadingBar defaultSize(Viewport vp)
22     {
23         int[2] window = [vp.worldWidth, vp.worldHeight];
24 
25         LoadingBar ret = new LoadingBar(cast(int)(window[0] * 0.8), cast(int)(window[1] / 8), cast(int)(window[0] * 0.05));
26 
27         ret.x = (window[0] / 2) - (ret.width / 2);
28         ret.y = (window[1] / 2) - (ret.height / 2);
29 
30         return ret;
31     }
32 
33     void setPosition(int x, int y)
34     {
35         this.x = x;
36         this.y = y;
37     }
38 
39     /** 
40      * Percentage must be a value between 0 and 1
41      */
42     void setPercentage(float percentage)
43     {
44         import hip.math.utils;
45         this.percentage = clamp(percentage, 0, 1);
46     }
47 
48     void render()
49     {
50         fillRectangle(x, y, width, height, bgColor);
51 
52         int halfB = borderSize/2;
53         fillRectangle(x+halfB, y+halfB, cast(int)((width-borderSize)*percentage), (height - borderSize), fgColor);
54 
55         drawRectangle(x+halfB, y+halfB, (width-borderSize), (height - borderSize), fgColor);
56 
57     }
58 }